package com.zhangdi.demo.text;

import com.huaban.analysis.jieba.JiebaSegmenter;
import java.util.HashSet;
import java.util.Set;

public class JaccardSimilarityJieba {

    private static final JiebaSegmenter segmenter = new JiebaSegmenter();

    public double getSimilarity(String name1, String name2) {
        Set<String> words1 = new HashSet<>(segmenter.sentenceProcess(name1));
        Set<String> words2 = new HashSet<>(segmenter.sentenceProcess(name2));
        Set<String> union = new HashSet<>(words1);
        union.addAll(words2);
        Set<String> intersection = new HashSet<>(words1);
        intersection.retainAll(words2);
        return (double) intersection.size() / (double) union.size();
    }

    public static void main(String[] args) {
        JaccardSimilarityJieba ccc = new JaccardSimilarityJieba();
        System.out.println(ccc.getSimilarity("莘县河店镇贾庄村美丽乡村一期规划", "莘县河店镇贾庄村美丽乡村三期规划"));
    }
}
